package com.huhu.levelloader;

import java.util.ArrayList;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.IPathModifierListener;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.tmx.TMXObject;
import org.andengine.extension.tmx.TMXObjectGroup;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.huhu.manager.ResourcesManager;
import com.huhu.manager.SceneManager;
import com.huhu.objects.Player;
import com.huhu.objects.TextSprite;
import com.huhu.scenes.GameScene;

public class LevelLoader1_1 extends LevelLoader{
	private HUD gameHUD;

	private Player player;
	private Sprite sprite_ques;
	private Sprite sprite_cloud_normal;
	private Sprite sprite_black_box;
	private Body body_black_box;
	private Sprite sprite_stage_hello;
	private Body body_stage_hello;
	private Sprite spriteSawLeft;
	private Sprite spriteSawRight;
	private boolean sawActive = false;
	private TextSprite textU;
	private TextSprite textNone;
	private TextSprite textI;
	private TextSprite textE;
	private TextSprite textD;
	private Sprite rocketTop;
	private boolean doRocketTop = false;
	private Sprite rocketLeft;
	private Sprite rocketRight;
	private Sprite hideBox;
	private Body hideBoxBody;
	private boolean levelCompleteDisplayed = false;
	private boolean gameOverDisplayed = false;

	private int countTextSprite = 0;

	public LevelLoader1_1(Engine engine, GameScene scene, BoundCamera camera,
			PhysicsWorld physicsWorld, VertexBufferObjectManager vbom) {
		super(engine, scene, camera, physicsWorld, vbom);
	}
	
	@Override
	protected void createHUD() {
		gameHUD = new HUD();

		final Sprite spriteMoveLeft = new Sprite(10, 520,
				resourcesManager.move_left_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(false);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveLeft);
		gameHUD.attachChild(spriteMoveLeft);

		final Sprite spriteMoveRight = new Sprite(135, 520,
				resourcesManager.move_right_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(true);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveRight);
		gameHUD.attachChild(spriteMoveRight);

		final Sprite spriteJump = new Sprite(876, 520,
				resourcesManager.jump_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					player.jump();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteJump);
		gameHUD.attachChild(spriteJump);

		final Sprite spriteBack = new Sprite(10, 10,
				resourcesManager.back_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteBack);
		gameHUD.attachChild(spriteBack);

		camera.setHUD(gameHUD);
		
	}

	public Player createPlatform(TMXTiledMap mTMXTiledMap) {
		final ArrayList<TMXObjectGroup> mTMXObjectGroups = mTMXTiledMap
				.getTMXObjectGroups();
		final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(
				0.0f, 0.01f, 0.5f);
		for (TMXObjectGroup mTMXObjectGroup : mTMXObjectGroups) {
			if (mTMXObjectGroup.getTMXObjectGroupProperties()
					.containsTMXProperty("platformobject", "true")) {
				for (TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()) {
					Rectangle rectangle = new Rectangle(mTMXObject.getX(),
							mTMXObject.getY(), mTMXObject.getWidth(),
							mTMXObject.getHeight(), vbom);
					Body body_platform = PhysicsFactory.createBoxBody(
							physicsWorld, rectangle, BodyType.StaticBody,
							FIXTURE_DEF);
					body_platform.setUserData("body_platform_data");
					rectangle.setVisible(false);
					this.scene.attachChild(rectangle);
				}
			} else if (mTMXObjectGroup.getTMXObjectGroupProperties()
					.containsTMXProperty("dynamicplatformobject", "true")) {
				for (TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()) {
					Sprite sprite_dynamic_platform = new Sprite(
							mTMXObject.getX(), mTMXObject.getY(),
							resourcesManager.dynamic_platform_region, vbom);
					Body body_dynamic_platform = PhysicsFactory.createBoxBody(
							physicsWorld, sprite_dynamic_platform,
							BodyType.StaticBody, FIXTURE_DEF);
					body_dynamic_platform
							.setUserData("body_dynamic_platform_data");
					this.scene.attachChild(sprite_dynamic_platform);
					physicsWorld.registerPhysicsConnector(new PhysicsConnector(
							sprite_dynamic_platform, body_dynamic_platform,
							true, false));
				}
			} else if (mTMXObjectGroup.getTMXObjectGroupProperties()
					.containsTMXProperty("propsobject", "true")) {
				for (TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()) {
					if (mTMXObject.getName().equals("ques")) {
						resourcesManager.loadUnknownGraphics();
						sprite_ques = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.unknown_region, vbom);
						Body body_ques = PhysicsFactory.createBoxBody(
								physicsWorld, sprite_ques, BodyType.StaticBody,
								FIXTURE_DEF);
						body_ques.setUserData("body_ques_data");
						this.scene.attachChild(sprite_ques);
					}

					if (mTMXObject.getName().equals("cloud")) {
						resourcesManager.loadCloudGraphics();
						sprite_cloud_normal = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.cloud_normal_region, vbom) {

							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);

								if (collidesWith(player)) {
									resourcesManager.replaceCloudTexture();
									player.setDie(true);
								}
							}
						};
						this.scene.attachChild(sprite_cloud_normal);
					}

					if (mTMXObject.getName().equals("glass")) {
						Rectangle rectangle = new Rectangle(mTMXObject.getX(),
								mTMXObject.getY(), mTMXObject.getWidth(),
								mTMXObject.getHeight(), vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									Sprite sprite_saw_top_red = new Sprite(
											0,
											0,
											resourcesManager.saw_top_red_region,
											vbom);
									sprite_saw_top_red.setPosition(
											getX(),
											getY()
													+ getHeight()
													- sprite_saw_top_red
															.getHeight());
									scene.attachChild(sprite_saw_top_red);
									player.setDie(true);
								}
							}
						};
						rectangle.setVisible(false);
						this.scene.attachChild(rectangle);
					}

					if (mTMXObject.getName().equals("blackbox")) {
						sprite_black_box = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.unknow_brickover_region, vbom);
						body_black_box = PhysicsFactory.createBoxBody(
								physicsWorld, sprite_black_box,
								BodyType.StaticBody, FIXTURE_DEF);
						body_black_box.setUserData("body_black_box_data");
						body_black_box.setActive(false);
						sprite_black_box.setVisible(false);
						this.scene.attachChild(sprite_black_box);
					}

					if (mTMXObject.getName().equals("recblackbox")) {
						final Rectangle recBlackBox = new Rectangle(
								mTMXObject.getX(), mTMXObject.getY(),
								mTMXObject.getWidth(), mTMXObject.getHeight(),
								vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									if (!body_black_box.isActive()) {
										body_black_box.setActive(true);
									}
								} else if (!sprite_black_box.isVisible()) {
									body_black_box.setActive(false);
								}
							}
						};
						recBlackBox.setVisible(false);
						scene.attachChild(recBlackBox);
					}

					if (mTMXObject.getName().equals("stagehello")) {
						sprite_stage_hello = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.stage1_1_hello, vbom);
						body_stage_hello = PhysicsFactory.createBoxBody(
								physicsWorld, sprite_stage_hello,
								BodyType.StaticBody, FIXTURE_DEF);
						body_stage_hello.setUserData("body_stage_hello_data");
						body_stage_hello.setActive(false);
						sprite_stage_hello.setVisible(false);
						this.scene.attachChild(sprite_stage_hello);
						physicsWorld
								.registerPhysicsConnector(new PhysicsConnector(
										sprite_stage_hello, body_stage_hello,
										true, false));
					}

					if (mTMXObject.getName().equals("dangerous")) {
						Rectangle rectangle = new Rectangle(mTMXObject.getX(),
								mTMXObject.getY(), mTMXObject.getWidth(),
								mTMXObject.getHeight(), vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (sprite_stage_hello.isVisible()) {
									if (collidesWith(player)) {
										body_stage_hello
												.setType(BodyType.DynamicBody);
									}
								}
							}
						};
						rectangle.setVisible(false);
						this.scene.attachChild(rectangle);
					}

					if (mTMXObject.getName().equals("sawleft")) {
						spriteSawLeft = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.saw_left_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);

								if (collidesWith(player)) {
									player.setDie(true);
								}
							}
						};
					}

					if (mTMXObject.getName().equals("sawright")) {
						spriteSawRight = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.saw_right_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);

								if (collidesWith(player)) {
									player.setDie(true);
								}
							}
						};

					}

					if (mTMXObject.getName().equals("keyobject")) {
						final Sprite spriteKey = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(), resourcesManager.key_region,
								vbom) {

							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);

								if (collidesWith(player)) {
									if (!sawActive) {
										scene.attachChild(spriteSawLeft);
										scene.attachChild(spriteSawRight);
										sawActive = true;
									}
								}
							}
						};
						scene.attachChild(spriteKey);
					}

					if (mTMXObject.getName().equals("saveobject")) {
						SceneManager.savePositionX = mTMXObject.getX();
						SceneManager.savePositionY = mTMXObject.getY();
						final Sprite spriteSave = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.save_flat_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									SceneManager.saveActivte = true;
									this.setVisible(false);
								}
							}
						};
						scene.attachChild(spriteSave);
					}

					if (mTMXObject.getName().equals("recudie")) {
						final Rectangle rec = new Rectangle(mTMXObject.getX(),
								mTMXObject.getY(), mTMXObject.getWidth(),
								mTMXObject.getHeight(), vbom) {

							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);

								if (collidesWith(player)) {
									textU.setBodyActive(true);
									textNone.setBodyActive(true);
									textD.setBodyActive(true);
									textI.setBodyActive(true);
									textE.setBodyActive(true);
								}
							}
						};

						scene.attachChild(rec);
					}

					if (mTMXObject.getName().equals("rockettop")) {
						float x = mTMXObject.getX();
						float y = mTMXObject.getY();
						rocketTop = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.rocket_top_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									player.setDie(true);
								}
							}
						};

						final Path path = new Path(2).to(x, y).to(x, 0);
						rocketTop.registerEntityModifier(new PathModifier(1,
								path, new IPathModifierListener() {

									@Override
									public void onPathWaypointStarted(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathWaypointFinished(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathStarted(
											PathModifier pPathModifier,
											IEntity pEntity) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathFinished(
											PathModifier pPathModifier,
											IEntity pEntity) {
										if (rocketTop.isVisible()) {
											rocketTop.setVisible(false);
											rocketTop = null;
										}

									}
								}));
					}

					if (mTMXObject.getName().equals("recrockettop")) {
						final Rectangle recRocket = new Rectangle(
								mTMXObject.getX(), mTMXObject.getY(),
								mTMXObject.getWidth(), mTMXObject.getHeight(),
								vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									engine.registerUpdateHandler(new TimerHandler(
											0.2f, new ITimerCallback() {

												@Override
												public void onTimePassed(
														TimerHandler pTimerHandler) {
													engine.unregisterUpdateHandler(pTimerHandler);
													if (!doRocketTop) {
														doRocketTop = true;
														scene.attachChild(rocketTop);
													}
												}
											}));
								}
							}
						};
						recRocket.setVisible(false);
						scene.attachChild(recRocket);
					}

					if (mTMXObject.getName().equals("rocketleft")) {
						float x = mTMXObject.getX();
						float y = mTMXObject.getY();
						rocketLeft = new Sprite(x, y,
								resourcesManager.rocket_left_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									player.setDie(true);
								}
							}
						};
						final Path path = new Path(2).to(x, y)
								.to(x + 8 * 64, y);
						rocketLeft.registerEntityModifier(new PathModifier(20,
								path, new IPathModifierListener() {

									@Override
									public void onPathWaypointStarted(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathWaypointFinished(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathStarted(
											PathModifier pPathModifier,
											IEntity pEntity) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathFinished(
											PathModifier pPathModifier,
											IEntity pEntity) {
										if (rocketLeft.isVisible()) {
											rocketLeft.setVisible(false);
											rocketLeft = null;
										}

									}
								}));
					}
					if (mTMXObject.getName().equals("rocketright")) {
						float x = mTMXObject.getX();
						float y = mTMXObject.getY();
						rocketRight = new Sprite(x, y,
								resourcesManager.rocket_right_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									player.setDie(true);
								}
							}
						};
						final Path path = new Path(2).to(x, y)
								.to(x - 8 * 64, y);
						rocketRight.registerEntityModifier(new PathModifier(20,
								path, new IPathModifierListener() {

									@Override
									public void onPathWaypointStarted(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathWaypointFinished(
											PathModifier pPathModifier,
											IEntity pEntity, int pWaypointIndex) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathStarted(
											PathModifier pPathModifier,
											IEntity pEntity) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onPathFinished(
											PathModifier pPathModifier,
											IEntity pEntity) {
										if (rocketRight.isVisible()) {
											rocketRight.setVisible(false);
											rocketRight = null;
										}

									}
								}));

					}
					if (mTMXObject.getName().equals("u")) {
						textU = new TextSprite(mTMXObject.getX(),
								mTMXObject.getY(), "U", vbom, physicsWorld,
								player);
						textU.setVisible(false);
						scene.attachChild(textU);
					}
					if (mTMXObject.getName().equals("none")) {
						textNone = new TextSprite(mTMXObject.getX(),
								mTMXObject.getY(), "", vbom, physicsWorld,
								player);
						textNone.setVisible(false);
						scene.attachChild(textNone);
					}
					if (mTMXObject.getName().equals("d")) {
						textD = new TextSprite(mTMXObject.getX(),
								mTMXObject.getY(), "D", vbom, physicsWorld,
								player);
						textD.setVisible(false);
						scene.attachChild(textD);
					}
					if (mTMXObject.getName().equals("i")) {
						textI = new TextSprite(mTMXObject.getX(),
								mTMXObject.getY(), "I", vbom, physicsWorld,
								player);
						textI.setVisible(false);
						scene.attachChild(textI);
					}
					if (mTMXObject.getName().equals("e")) {
						textE = new TextSprite(mTMXObject.getX(),
								mTMXObject.getY(), "E", vbom, physicsWorld,
								player);
						textE.setVisible(false);
						scene.attachChild(textE);
					}

					if (mTMXObject.getName().equals("hidebox")) {
						hideBox = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.unknow_brickover_region, vbom);
						hideBoxBody = PhysicsFactory.createBoxBody(
								physicsWorld, hideBox, BodyType.StaticBody,
								FIXTURE_DEF);
						hideBoxBody.setUserData("hide_box_body_data");
						hideBoxBody.setActive(false);
						hideBox.setVisible(false);
						scene.attachChild(hideBox);
					}

					if (mTMXObject.getName().equals("rechidebox")) {
						final Rectangle rec = new Rectangle(mTMXObject.getX(),
								mTMXObject.getY(), mTMXObject.getWidth(),
								mTMXObject.getHeight(), vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									if (!hideBoxBody.isActive()) {
										hideBoxBody.setActive(true);
									}
								} else {
									if (!hideBox.isVisible()) {
										hideBoxBody.setActive(false);
									}
								}
							}
						};
						rec.setVisible(false);
						scene.attachChild(rec);
					}

					if (mTMXObject.getName().equals("lady")) {
						final Sprite spriteLady = new Sprite(mTMXObject.getX(),
								mTMXObject.getY(),
								resourcesManager.lady_region, vbom) {
							@Override
							protected void onManagedUpdate(float pSecondsElapsed) {
								// TODO Auto-generated method stub
								super.onManagedUpdate(pSecondsElapsed);
								if (collidesWith(player)) {
									if (!levelCompleteDisplayed) {
										levelCompleteDisplayed = true;
										scene.displayLevelComplete(SceneManager.currentLevel);
										// levelCompleteWindow.display(0, scene,
										// camera);
									}
								}
							}
						};
						scene.attachChild(spriteLady);
					}
				}
			} else if (mTMXObjectGroup.getTMXObjectGroupProperties()
					.containsTMXProperty("playerobject", "true")) {
				for (TMXObject mTMXObject : mTMXObjectGroup.getTMXObjects()) {
					if(!SceneManager.saveActivte){
						SceneManager.savePositionX = mTMXObject.getX();
						SceneManager.savePositionY = mTMXObject.getY();
					}
					player = new Player(SceneManager.savePositionX, SceneManager.savePositionY,
							vbom, camera, physicsWorld) {

						@Override
						public void onDie() {
							if (!gameOverDisplayed) {
								gameOverDisplayed = true;
								if (!ResourcesManager.getInstance().die_sound
										.isPlaying()) {
									ResourcesManager.getInstance().die_sound
											.play();
								}
								scene.displayGamOver();
							}

						}
					};

					scene.attachChild(player);
				}
			}
		}
		return player;
	}

	protected ContactListener contactListener() {
		ContactListener contactListener = new ContactListener() {

			@Override
			public void preSolve(Contact contact, Manifold oldManifold) {
				// TODO Auto-generated method stub

			}

			@Override
			public void postSolve(Contact contact, ContactImpulse impulse) {
				// TODO Auto-generated method stub

			}

			@Override
			public void endContact(Contact contact) {

				final Fixture x1 = contact.getFixtureA();
				final Fixture x2 = contact.getFixtureB();
				//
				if (x1.getBody().getUserData() != null
						&& x2.getBody().getUserData() != null) {
					String x1_bodyData = x1.getBody().getUserData().toString();
					String x2_bodyData = x2.getBody().getUserData().toString();

					if (x1_bodyData.equals("player")
							|| x2_bodyData.equals("player")) {
						player.decreaseFootContacts();
						Log.d("Contact", "End " + player.footContact);

						// if (x1_bodyData.equals("body_platform_data")
						// || x2_bodyData.equals("body_platform_data")) {
						// if ((player.getX() >= sprite_black_box.getX() - 32)
						// && (player.getY() >= sprite_black_box
						// .getY())) {
						// if (!body_black_box.isActive()) {
						// body_black_box.setActive(true);
						// }
						// } else {
						// if (!sprite_black_box.isVisible()) {
						// body_black_box.setActive(false);
						// }
						// }
						// }
					}
				}

			}

			@Override
			public void beginContact(Contact contact) {
				final Fixture x1 = contact.getFixtureA();
				final Fixture x2 = contact.getFixtureB();
				Log.d("userdata", x1.getBody().getUserData() + " "
						+ x2.getBody().getUserData());

				if (x1.getBody().getUserData() != null
						&& x2.getBody().getUserData() != null) {
					final Object x1_bodyData = x1.getBody().getUserData()
							.toString();
					final Object x2_body_Data = x2.getBody().getUserData()
							.toString();

					if (x1.getBody().getUserData().equals("player")
							|| x2.getBody().getUserData().equals("player")) {
						player.increaseFootContacts();
						Log.d("Contact", "Begin " + player.footContact);

						if (x1.getBody().getUserData().equals("body_ques_data")
								|| x2.getBody().getUserData()
										.equals("body_ques_data")) {

							if (player.getY() >= sprite_ques.getY()
									+ sprite_ques.getHeight()) {
								Log.d("SpriteQues",
										"Collides: " + player.getY() + " "
												+ sprite_ques.getY());
								resourcesManager.replaceUnknownTexture();
								sprite_stage_hello.setVisible(true);
								body_stage_hello.setActive(true);
							}
						} else if (x1_bodyData.equals("body_stage_hello_data")
								|| x2_body_Data.equals("body_stage_hello_data")) {
							Log.d("Stage", "Cham");
							Log.d("Stage", player.getY() + " "
									+ sprite_stage_hello.getY()
									+ sprite_stage_hello.getHeight());
							if (player.getY() >= sprite_stage_hello.getY()
									+ sprite_stage_hello.getHeight() / 2) {
								player.setDie(true);
							}
						} else if (x1_bodyData.equals("body_black_box_data")
								|| x2_body_Data.equals("body_black_box_data")) {
							if (!sprite_black_box.isVisible()) {
								sprite_black_box.setVisible(true);
							}
						} else if (x1_bodyData
								.equals("body_dynamic_platform_data")
								|| x2_body_Data
										.equals("body_dynamic_platform_data")) {
							engine.registerUpdateHandler(new TimerHandler(0.1f,
									new ITimerCallback() {

										@Override
										public void onTimePassed(
												TimerHandler pTimerHandler) {
											engine.registerUpdateHandler(pTimerHandler);
											if (x1_bodyData
													.equals("body_dynamic_platform_data")) {
												x1.getBody().setType(
														BodyType.DynamicBody);
											} else {
												x2.getBody().setType(
														BodyType.DynamicBody);
											}
										}
									}));
						} else if (x1_bodyData.equals("U")
								|| x2_body_Data.equals("U")) {
							if (!textU.isVisible()) {
								textU.setVisible(true);
								countTextSprite++;
								Log.d("udie", countTextSprite + "");
							}
						} else if (x1_bodyData.equals("")
								|| x2_body_Data.equals("")) {
							if (!textNone.isVisible()) {
								textNone.setVisible(true);
								countTextSprite++;
								Log.d("udie", countTextSprite + "");
							}
						} else if (x1_bodyData.equals("D")
								|| x2_body_Data.equals("D")) {
							if (!textD.isVisible()) {
								textD.setVisible(true);
								countTextSprite++;
								Log.d("udie", countTextSprite + "");
							}
						} else if (x1_bodyData.equals("I")
								|| x2_body_Data.equals("I")) {
							if (!textI.isVisible()) {
								textI.setVisible(true);
								countTextSprite++;
								Log.d("udie", countTextSprite + "");
							}
						} else if (x1_bodyData.equals("E")
								|| x2_body_Data.equals("E")) {
							if (!textE.isVisible()) {
								textE.setVisible(true);
								countTextSprite++;
								Log.d("udie", countTextSprite + "");
							}
						} else if (x1_bodyData.equals("hide_box_body_data")
								|| x2_body_Data.equals("hide_box_body_data")) {
							if (!hideBox.isVisible()) {
								hideBox.setVisible(true);
							}
						}

						if (countTextSprite == 5) {
							countTextSprite++;
							doRocket();
						}
					}
				}
			}
		};

		return contactListener;
	}

	private void doRocket() {
		scene.attachChild(rocketLeft);
		scene.attachChild(rocketRight);
	}
}
